

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_hash implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);

		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}

		if (extra.length != 0) {
			if (extra[0] != " ") {
				String tosend = null;
				String toencode = null;
				for (String s : extra) {
					if (toencode == null) {
						toencode = s;
					} else {
						toencode = toencode + " " + toencode;
					}
				}
				String s = toencode;

				// System.out.println("MD5: "+new
				// BigInteger(1,m.digest()).toString(16));
				MessageDigest md1 = null;
				try {
					md1 = MessageDigest.getInstance("MD5");
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				md1.update(toencode.getBytes());
				byte[] output = md1.digest();

				String md5 = bytesToHex(output);

				MessageDigest md = null;
				try {
					md = MessageDigest.getInstance("SHA1");
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				md.update(toencode.getBytes());
				output = md.digest();
				String sha1 = bytesToHex(output);

				MessageDigest md2 = null;
				try {
					md2 = MessageDigest.getInstance("SHA-256");
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				md2.update(toencode.getBytes());
				byte[] output2 = md2.digest();
				String sha256 = bytesToHex(output2);

				tosend = "MD5: " + md5.toLowerCase() + ", SHA1: "
						+ sha1.toLowerCase() + ", SHA256: "
						+ sha256.toLowerCase();
				// address.getCanonicalHostName()
				// tosend = " seems to be up.";

				
				irc.sendMessage(channel, start + "" + tosend);
			} else {
				
				irc.sendMessage(channel, start
						+ "You need to specify some input!");
			}
		} else {
			
			irc.sendMessage(channel, start + "You need to specify some input!");
		}
	}

	public String help() {
		return "Hash - hash [input] - Hashes the input in MD5, SHA1 and SHA256.";
		// TODO Auto-generated method stub

	}

	// public static boolean exists(String URLName){
	// try {
	// HttpURLConnection.setFollowRedirects(true);
	// // note : you may also need
	// //HttpURLConnection.setInstanceFollowRedirects(false)
	// HttpURLConnection con =
	// (HttpURLConnection) new URL(URLName).openConnection();
	// con.setRequestMethod("HEAD");
	// return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	// }
	// catch (Exception e) {
	// e.printStackTrace();
	// return false;
	// }
	// }
	public String version() {
		return "Hash version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	public static String bytesToHex(byte[] b) {
		char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		StringBuffer buf = new StringBuffer();
		for (int j = 0; j < b.length; j++) {
			buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
			buf.append(hexDigit[b[j] & 0x0f]);
		}
		return buf.toString();
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "md5, sh1, sha256, hashthis, hashit";
	}

}
